package com.example.qxfw.nyqx.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.ny.CropyieldHistory;
import com.example.qxfw.nyqx.mapper.CropyieldHistoryMapper;
import com.example.qxfw.nyqx.service.CropyieldHistoryService;
import com.example.qxfw.nyqx.vo.CropYieldHistoryListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class   CropyieldHistoryServiceImpl implements CropyieldHistoryService {

    @Autowired
    private CropyieldHistoryMapper cropyieldHistoryMapper;

    @Override
    public List<CropyieldHistory> getList(String year1, String year2, String stationId, String type) {
        return cropyieldHistoryMapper.selectList2(year1,year2,stationId,type);
    }

    @Override
    public List<CropyieldHistory> getList1(String year1, String year2, String stationId, String type, String cropName) {
        return cropyieldHistoryMapper.selectList1(year1, year2, stationId, type, cropName);
    }

    @Override
    public List<CropyieldHistory> listByParams(String stationId, String cropName, String type, long year1, long year2) {
        return cropyieldHistoryMapper.selectList(Wrappers.<CropyieldHistory>lambdaQuery()
                .eq(CropyieldHistory::getStationid,stationId)
                .eq(CropyieldHistory::getCropname,cropName)
                .eq(StringUtils.isNotBlank(type),CropyieldHistory::getType,type)
                .ge(CropyieldHistory::getYear,year1)
                .le(CropyieldHistory::getYear,year2)
                .orderByAsc(CropyieldHistory::getYear));
    }

    @Override
    public List<String> getYield(String year, String stationId, String type) {
        return cropyieldHistoryMapper.selectYield(year, stationId, type);
    }

    @Override
    public List<String> getYield1(String year, String stationId) {
        return cropyieldHistoryMapper.selectYield1(year,stationId);
    }

    @Override
    public List<String> getYield2(String year1, String stationId, String type, String cropName) {
        return cropyieldHistoryMapper.selectYield2(year1, stationId, type, cropName);
    }

    @Override
    public List<String> getYield3(String year1, String stationId, String cropName) {
        return cropyieldHistoryMapper.selectYield3(year1,stationId,cropName);
    }

    @Override
    public List<Double> listYieldByParam(long year, String stationId, String type, String cropName) {
        List<CropyieldHistory> cropyieldHistories = cropyieldHistoryMapper.selectList(Wrappers.<CropyieldHistory>lambdaQuery()
                .eq(CropyieldHistory::getStationid, stationId)
                .eq(CropyieldHistory::getCropname, cropName)
                .eq(StringUtils.isNotBlank(type), CropyieldHistory::getType, type)
                .isNull(StringUtils.isBlank(type), CropyieldHistory::getType)
                .eq(CropyieldHistory::getYear, year));
        List<Double> yields = new ArrayList<>();
        for (CropyieldHistory cropyieldHistory : cropyieldHistories) {
            yields.add(cropyieldHistory.getYield());
        }
        return yields;
    }

    @Override
    public List<CropYieldHistoryListVo> listByYear(long year1, long year2, String stationId, String type, String cropName) {
        return cropyieldHistoryMapper.listByYear(year1, year2, stationId, type, cropName);
    }


}
